import minimist from "minimist";
import { Logger, logger } from "./logger";
import { container } from "tsyringe";
import { ENV, REQUIRE, ROOT } from "./tokens";
import { skerReq } from "./module";
import { config } from 'dotenv'
export function init() {
    config();
    const parsedArgs = minimist(process.argv.slice(2), {
        boolean: ['help', 'dev', 'prod'],
        string: ['host', 'port', `themes`, 'scope', 'addon', 'cpus', `orm`],
        alias: { help: 'h', version: 'v', cpus: 'c' }
    });
    parsedArgs.host = parsedArgs.host || process.env.HOST || '0.0.0.0';
    parsedArgs.port = parsedArgs.port || process.env.PORT || `3102`;
    parsedArgs.themes = parsedArgs.themes || process.env.THEMES || 'default'
    if(process.env?.NODE_ENV){
        parsedArgs.dev = process.env.NODE_ENV === 'development'
    }
    parsedArgs.prod = !parsedArgs.dev;
    parsedArgs.dev = !parsedArgs.prod;
    parsedArgs.orm = !!parsedArgs.orm;
    parsedArgs.cpus = parseInt(parsedArgs.cpus || `1`)
    const env = {
        ...process.env,
        ...parsedArgs
    }
    container.register(ENV, { useValue: env })
    container.register(ROOT, { useValue: process.cwd() })
    container.register(REQUIRE, {
        useValue: (id: string, paths: string[] = []) => {
            return skerReq(paths, container)(id)
        }
    });
    container.register(Logger, { useValue: logger })
    return parsedArgs;
}